package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月12日 19:29
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * @Author: ggm
     * @date: 2024/11/12
     * 套餐分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.打印日志
        log.info("套餐分页查询:{}", setmealPageQueryDTO);
        //2.调用service层
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        //3,返回结果
        return Result.success(pageResult);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/12
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐:{}", setmealDTO);
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * @Author: ggm
     * @date: 2024/11/14
     * 批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam(value = "ids") List<Long> ids) {
        //1,打印日志
        log.info("批量删除:{}", ids);
        //2,调用service层
        setmealService.delete(ids);
        //3,返回结果
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/14
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/29
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐:{}", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);


    }

    /**
     * @Author: ggm
     * @date: 2024/11/29
     * 套餐起售停售
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable("status") Integer status, Long id) {
        log.info("套餐起售停售:{},{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();

    }


}
